import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { RoleDto } from 'src/app/models/sys/role.dto';
import { BehaviorSubject } from 'rxjs';
import { ApiService } from 'src/app/services/api.service';
import { Consts } from 'src/app/consts/consts';
import { DataScopeTypeEnum } from 'src/app/models/enums/data-scope-type.enum';

@Component({
  selector: 'app-role-detail',
  templateUrl: './role-detail.component.html',
  styleUrl: './role-detail.component.less'
})
export class RoleDetailComponent implements OnInit  {
  @Input() roleSubject = new BehaviorSubject<RoleDto>(new RoleDto());
  @Output() detailEvent = new EventEmitter<boolean>();

  role?: RoleDto;

  constructor(private _api: ApiService) {

  }

  ngOnInit(): void {
    this.roleSubject.subscribe((value) => {
      this._api.getRoleDetail(value.roleId).subscribe((resp) => {
        if (resp.code == Consts.RESP_OK && resp.data) {
          this.role = resp.data;

          this.role.dataScopeName = this.dataScopeName(this.role.dataScope);
        }
      });
    });
  }

  onClose(event: UIEvent) {
    event.preventDefault();

    this.detailEvent.emit(true);
  }

  dataScopeName(dataScope: number | undefined): string | undefined {
    if (dataScope == DataScopeTypeEnum.ALL_DATA) {
      return '全部数据';
    } else if (dataScope == DataScopeTypeEnum.CUSTOM_DATA) {
      return '自定义数据';
    } else if (dataScope == DataScopeTypeEnum.LOCAL_DEPT) {
      return '本部门数据';
    } else if (dataScope == DataScopeTypeEnum.LOCAL_DEPT_TREE) {
      return '本部门及下级部门数据';
    } else if (dataScope == DataScopeTypeEnum.SELF) {
      return '个人数据';
    }

    return undefined;
  }
}
